﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarAgeDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarAgeDB
    {
        public CarAgeDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarAge CarAge)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarAgeInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarAgeId", CarAge.ProviderCarAgeId);
            _sPara.Add("@ProviderCompanyId", CarAge.ProviderCompanyId);
            _sPara.Add("@FromAge", CarAge.FromAge);
            _sPara.Add("@ToAge", CarAge.ToAge);
            _sPara.Add("@Rate", CarAge.Rate);
            _sPara.Add("@RefCEUserId", CarAge.RefCEUserId);
            _sPara.Add("@Status", CarAge.Status);
            _sPara.Add("@Finfo1", CarAge.FInfo1);
            _sPara.Add("@Dinfo1", CarAge.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarAge GetProviderCarAge(Int64 ProviderCarAgeId)
        {
            CarAge _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarAgeSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarAgeId", ProviderCarAgeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarAge();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarAge GetProviderCarAge(Int64 ProviderCompanyId,Int32 FromAge, Int32 ToAge)
        {
            CarAge _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarAgeSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@FromAge", FromAge);
            _spara.Add("@ToAge", ToAge);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarAge();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarAge FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarAge myCarAge = new CarAge();
            myCarAge.ProviderCarAgeId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarAgeId"].ToString());
            myCarAge.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarAge.FromAge = Convert.ToInt32(myDataTable.Rows[RowId]["FromAge"].ToString());
            myCarAge.ToAge = Convert.ToInt32(myDataTable.Rows[RowId]["ToAge"].ToString());
            myCarAge.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarAge.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarAge.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarAge.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarAge.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarAge.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarAge.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarAge;
        }
    }
}
